<?php

class UserController extends Controller
{
    public function actionAvatar(){
        $images = CUploadedFile::getInstanceByName('avatar');
        if($images->name === 'blob'){
            $newName = md5(microtime(true)).".jpg";
            $images->saveAs('uploads/' . $newName);
        }

    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionChangePassword()
    {
        $model = new ChangePasswordForm;

        //check ajax request
        if(Yii::app()->request->isAjaxRequest && isset($_POST['ChangePasswordForm'])){
            $model->attributes=$_POST['ChangePasswordForm'];
            if(!$model->validate()){
                $rs =  CActiveForm::validate($model);
                echo CJSON::encode(array("resultCd" => "03", "msg" => CJSON::decode($rs)));
            }else{
                $user = User::model()->findByPk(Yii::app()->user->getId());
                $user->password = CPasswordHelper::hashPassword($_POST['ChangePasswordForm']['newPassword']);
                $user->update();
                echo CJSON::encode(array("resultCd" => "00", "msg" => ""));
            }
            Yii::app()->end();
        }
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->renderPartial("changePassword", array('model'=>$model),false, true);

    }

}